#!/bin/sh
FTP_SERVER_URL=ftp://192.168.0.18/
CC_BUILD_DETAIL_URL=http://192.168.0.18:8080/dashboard/tab/build/detail/

DVR_TYPE=
if [ $# -eq  0 ] ; then
	#echo "Default for ${DVR_TYPE}'s last version."
	echo "./wget.sh DVR_TYPE [FOLDER_NAME]"
	exit
fi

case $1 in
	"-h" | "--help")
	echo "./wget.sh DVR_TYPE [FOLDER_NAME]"
	echo "if no FOLDER NAME, will download all!."
	exit
	;;
	*)
	echo $DVR_TYPE
	DVR_TYPE=$1
	;;
esac


REV_FOLDER_LIST=`wget -q -c -O - ${CC_BUILD_DETAIL_URL}/${DVR_TYPE} |grep -e "${DRV_TYPE}_[[:digit:]]\+M$" -B1|grep "dashboard"|sed -e 's/.*\///g' -e 's/">//g' |head`
SVN_REV_LIST=`wget -q -c -O - ${CC_BUILD_DETAIL_URL}/${DVR_TYPE} |grep -e "${DVR_TYPE}_[[:digit:]]\+M$"|awk '{print $NF}'|head `
#echo "REV_FOLDER_LIST=${REV_FOLDER_LIST}"
#echo "SVN_REV_LIST=${SVN_REV_LIST}"
MAX_SVN_REV_COUNT=0
human_readable_date=""
cc_folder_name_to_date()
{
    cc_date=$1
    year=`echo ${cc_date}   | cut -c 1-4`
    month=`echo ${cc_date}  | cut -c 5-6`
    day=`echo ${cc_date}    | cut -c 7-8`
    hour=`echo ${cc_date}   | cut -c 9-10`
    minute=`echo ${cc_date} | cut -c 11-12`
    second=`echo ${cc_date} | cut -c 13-14`
    human_readable_date="${year}/${month}/${day}_${hour}:${minute}:${second}"
}

print_autobuild_lib_list()
{
    svn_rev_count=0
    for ver in ${SVN_REV_LIST}
    do
        printf "%-3s :\t%-10s\n" ${svn_rev_count} ${ver}
        let "svn_rev_count=svn_rev_count+1"
    done
    let "MAX_SVN_REV_COUNT = svn_rev_count"
}

print_autobuild_lib_list

while [ 1 ] ;
do
    printf "%s\n" "--------------------------------------"
    printf "%-3s :\tShow latest builds\n"       "s"
	printf "%-3s :\tList current dir\n"         "l"
	printf "%-3s :\tClear screen\n"             "c"
	printf "%-3s :\tQuit to main menu\n"        "q"
	printf "%-3s :\tDelete all exist lib\n"     "d"
	echo -n "Downlib:"
	read choose_ver_idx
	
	case ${choose_ver_idx} in
		"")
		continue
		;;
		
		"l")
		ls -l
		;;

		"c")
		clear
		;;

		"d")
	    echo "All libs deleted"
        rm -f ${HBLIB_PATH}/*
        rm -f ${SLAVE_CHIP_DIR}/`basename ${HBLIB_PATH}`/*
		;;

		"q")
	    echo "Exit ...."
		break
		;;

		"s")
        print_autobuild_lib_list
		;;

		[0-9]*)
        echo "input choice " ${choose_ver_idx}
		if [ ${choose_ver_idx} -gt ${MAX_SVN_REV_COUNT} ] ; then
			echo "The input '$choose_ver_idx' is error !"
			continue
		fi
		LIST_IDX_TMP=0
		for ver in ${REV_FOLDER_LIST}
		do
			if [ ${choose_ver_idx} -eq ${LIST_IDX_TMP} ] ; then
				if [ $# -eq 2 ] ; then
					echo "Download ${FTP_SERVER_URL}/${DVR_TYPE}/${ver}/$2"
					wget -m ${FTP_SERVER_URL}/${DVR_TYPE}/${ver}/$2  -nH -nv --cut-dirs=4 -P ${HBLIB_PATH}
                    wget -m ${FTP_SERVER_URL}/${DVR_TYPE}/${ver}/`basename ${SLAVE_CHIP_DIR}`/$2 \
                        -nH -nv --cut-dirs=5 -P ${SLAVE_CHIP_DIR}/`basename ${HBLIB_PATH}`
				else
					echo "Download ${FTP_SERVER_URL}/${DVR_TYPE}/${ver}"
					wget -m ${FTP_SERVER_URL}/${DVR_TYPE}/${ver}  -nH -nv --cut-dirs=2 -P ~
				fi
				break
			fi
			let LIST_IDX_TMP=LIST_IDX_TMP+1
		done
		;;
		
		*)
			echo "Unsupported input " ${choose_ver_idx}
			continue
		;;
	esac
done

# vim:et:sw=4:ts=4
